﻿using DevExpress.Utils.Win;
using DevExpress.XtraEditors.Popup;
using DevExpress.XtraGrid.Demos.util;
using DevExpress.XtraGrid.Editors;
using DevExpress.XtraLayout;
using DXApplicationTangche.DTO;
using DXApplicationTangche.service;
using DXApplicationTangche.UC.下单MTO.Service;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DXApplicationTangche.UC.订单一览新.销售确认单
{
    public partial class Form补充客户标签 : DevExpress.XtraEditors.XtraForm
    {
        private String year = "1900";
        private String month = "01-01";
        private String assistantId { get; set; }
        private String assistantName { get; set; }
        private String guestName { get; set; }
        private String guestMobile { get; set; }
        private CustomerPDTO customerPDTO = new CustomerPDTO();
        private PersonaDTO personaDTO = new PersonaDTO();
        public Form补充客户标签(String mobile)
        {
            InitializeComponent();
            this.personaDTO = 销售确认Service.findPersonaDTOByMobile(mobile);
            this.searchLookUpEdit销售员.Properties.DataSource = 销售确认Service.get销售员带助记码();
            this.searchLookUpEdit推荐人.Properties.DataSource = MTO订单Service.findAllCustomers();
            this.客户地址信息赋值(mobile);
            this.基础信息赋值();
        }
        #region 选择销售
        private void searchLookUpEdit销售员_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear销售员Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit销售员View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit销售员.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.assistantId = this.searchLookUpEdit销售员.Properties.View.GetRowCellValue(rowHandle, "id").ToString();
                    this.assistantName = this.searchLookUpEdit销售员.Properties.View.GetRowCellValue(rowHandle, "name").ToString();
                    this.personaDTO.buildSalesInfoAssistant(this.assistantId, this.assistantName);
                }
            }
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear销售员Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit销售员.ToolTip = null;
            searchLookUpEdit销售员.EditValue = null;
        }
        private void searchLookUpEdit销售员_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.assistantName;
        }
        private void searchLookUpEdit推荐人_Popup(object sender, EventArgs e)
        {
            PopupSearchLookUpEditForm form = (sender as IPopupControl).PopupWindow as PopupSearchLookUpEditForm;
            SearchEditLookUpPopup popup = form.Controls.OfType<SearchEditLookUpPopup>().FirstOrDefault();
            LayoutControl layout = popup.Controls.OfType<LayoutControl>().FirstOrDefault();
            //如果窗体内空间没有确认按钮，则自定义确认simplebutton，取消simplebutton，选中结果label
            if (layout.Controls.OfType<Control>().Where(ct => ct.Name == "btOK").FirstOrDefault() == null)
            {
                //得到空的空间
                EmptySpaceItem a = layout.Items.Where(it => it.TypeName == "EmptySpaceItem").FirstOrDefault() as EmptySpaceItem;

                //得到取消按钮，重写点击事件
                Control clearBtn = layout.Controls.OfType<Control>().Where(ct => ct.Name == "btClear").FirstOrDefault();
                LayoutControlItem clearLCI = (LayoutControlItem)layout.GetItemByControl(clearBtn);
                clearBtn.Click += clear推荐人Btn_Click;

                //添加一个simplebutton控件(确认按钮)
                LayoutControlItem myLCI = (LayoutControlItem)clearLCI.Owner.CreateLayoutItem(clearLCI.Parent);
                myLCI.TextVisible = false;
            }
        }
        private void searchLookUpEdit推荐人View_Click(object sender, EventArgs e)
        {
            int[] a = this.searchLookUpEdit推荐人.Properties.View.GetSelectedRows();
            if (a.Length > 0)
            {
                foreach (int rowHandle in a)
                {
                    this.guestName = this.searchLookUpEdit推荐人.Properties.View.GetRowCellValue(rowHandle, "customerName").ToString();
                    this.guestMobile = this.searchLookUpEdit推荐人.Properties.View.GetRowCellValue(rowHandle, "mobile").ToString();
                    this.personaDTO.buildSalesInfo(this.guestName,this.guestMobile);
                }
            }
            this.textBox推荐人姓名.Text = this.guestName;
            this.textBox推荐人电话.Text = this.guestMobile;
        }

        /// <summary>
        /// 清除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void clear推荐人Btn_Click(object sender, EventArgs e)
        {
            this.searchLookUpEdit推荐人.ToolTip = null;
            searchLookUpEdit推荐人.EditValue = null;
        }
        private void searchLookUpEdit推荐人_CustomDisplayText(object sender, DevExpress.XtraEditors.Controls.CustomDisplayTextEventArgs e)
        {
            if (null != e.Value)
                e.DisplayText = this.guestName;
        }
        #endregion
        private void 客户地址信息赋值(String mobile)
        {
            this.customerPDTO = MTO订单Service.getCustomerPByMobile(mobile);
            //this.textBox客户姓名.Text = this.customerPDTO.customerName;
            //this.textBox电话.Text = this.customerPDTO.mobile;
            this.textBox省.Text = this.customerPDTO.addressProvince;
            this.textBox市.Text = this.customerPDTO.addressCity;
            this.textBox区.Text = this.customerPDTO.addressDistrict;
            this.textBox详细地址.Text = this.customerPDTO.addressDetail;
        }
        private void dockPanel客户标签_Click(object sender, EventArgs e)
        {
            this.personaDTO.buildBasicInfo(
                this.textBox微信名.Text.Trim(),
                String.IsNullOrWhiteSpace(this.comboBox性别.Text)?2: this.comboBox性别.SelectedIndex
                );
            this.personaDTO.buildFigureInfo(
                this.textBox身高.Text.Trim(),
                this.textBox体重.Text.Trim()
                );
            String ret = 销售确认Service.savePersonaInfo(this.personaDTO);
            //补充标签后调用关闭defectCode接口
            销售确认Service.qcItemCloseByCustomer(this.textBox电话.Text.Trim(), "补充客户标签");
            MessageBox.Show("操作完成");
        }
        private void 基础信息赋值()
        {
            this.textBox电话.Text = this.personaDTO.mobile;
            this.textBox客户编码.Text = this.personaDTO.customerCode;
            this.textBox客户姓名.Text = this.personaDTO.customerName;
            this.textBox微信名.Text = this.personaDTO.wechatName;
            this.comboBox性别.SelectedIndex = this.personaDTO.gender;
            this.textBox生日.Text = this.personaDTO.birthday.ToString("yyyy-MM-dd");
            //
            this.textBox身高.Text = this.personaDTO.height.ToString();
            this.textBox体重.Text = this.personaDTO.weight.ToString();
            //
            this.textBox推荐人姓名.Text = this.personaDTO.recommenderMobile;
            this.textBox推荐人电话.Text = this.personaDTO.recommenderName;
            this.assistantId = this.personaDTO.salesConsultantId;
            this.assistantName = this.personaDTO.salesConsultantName;
            this.searchLookUpEdit销售员.Text = this.assistantName;
            //
            this.正面照片(this.personaDTO.frontPicUrl);
        }
        private void 正面照片(String frontPicUrl)
        {
            try
            {
                this.pictureBox正面照片.Image = FileService.GetImageByUrl(URL.图片url + URL.用户画像图片路径 + frontPicUrl);
            }
            catch
            {
                this.pictureBox正面照片.Image = DXApplicationTangche.Properties.Resources.persona;
            }
        }

        private void pictureBox正面照片_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Filter = "gif,jpg,png|*.gif;*.jpg;*.png;*.jpeg";
                ofd.Title = "请选择图片文件";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    String a = 销售确认Service.saveCustomerFrontPicture(this.personaDTO.mobile, ofd);
                    this.正面照片(a);
                    MessageBox.Show("操作完成");
                }
            }
            catch (Exception)
            {
                MessageBox.Show("操作失败");
            }
        }

        private void dateEdit年份_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                this.year = string.Format("{0:yyyy}", Convert.ToDateTime(this.dateEdit年份.EditValue.ToString()));
            }
            catch {
                this.year = "1900";
            }
            try
            {
                this.month = string.Format("{0:MM-dd}", Convert.ToDateTime(this.dateEdit月日.EditValue.ToString()));
            }
            catch
            {
                this.month = "01-01";
            }
            this.textBox生日.Text = this.year + "-" + this.month;
            this.personaDTO.buildBirthday(this.textBox生日.Text);
        }

        private void dateEdit月日_EditValueChanged(object sender, EventArgs e)
        {
            try
            {
                this.year = string.Format("{0:yyyy}", Convert.ToDateTime(this.dateEdit年份.EditValue.ToString()));
            }
            catch
            {
                this.year = "1900";
            }
            try
            {
                this.month = string.Format("{0:MM-dd}", Convert.ToDateTime(this.dateEdit月日.EditValue.ToString()));
            }
            catch
            {
                this.month = "01-01";
            }
            this.textBox生日.Text = this.year + "-" + this.month;
            this.personaDTO.buildBirthday(this.textBox生日.Text);
        }
    }
}
